package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.authentication.PermissionAuth;
import com.woniuxy.entitys.TbMalfunction;
import com.woniuxy.entitys.TbOrder;
import com.woniuxy.remote.RemoteHospitalService;
import com.woniuxy.services.OrderListService;
import com.woniuxy.utils.ResultVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname OrderController
 * @Date 2024/1/10 15:25
 */
@RestController
public class OrderController {

    @Autowired
    private OrderListService orderListService;
    @Autowired
    private RemoteHospitalService remoteHospitalService;


    @PostMapping("/order/list")
    public ResultVO getAllOrder(@RequestBody Map<String,Object> map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        List hospitals = (List) remoteHospitalService.getHospitalAndDepartment().getData();
        PageInfo<TbOrder> orderList = orderListService.selAllOrderList(pageNum, pageSize);
        Map<String, Object> response = new HashMap<>();
        response.put("orderList", orderList);
        response.put("hospitals", hospitals);
        return orderList!=null?ResultVO.success("success",response):ResultVO.failure("error");
    }
    @PostMapping("/hospital/key1")
    public ResultVO selOrderByHospital(@RequestBody Map<String,Object> map){

        Map pageInfo = (Map) map.get("pageInfo");

        Integer pageSize = (Integer)pageInfo.get("pageSize");
        Integer pageNum = (Integer)pageInfo.get("pageNum");
        Integer key1 = (Integer) map.get("key1");
        PageInfo<TbOrder> pageInfo1 = orderListService.selOrderByHospital(pageNum,pageSize,key1);
        return pageInfo1!=null?ResultVO.success("success",pageInfo1):ResultVO.failure("error");
    }

    @PostMapping("/department/key2")
    public ResultVO selOrderByDepartment(@RequestBody Map<String,Object> map){

        Map pageInfo = (Map) map.get("pageInfo");

        Integer pageSize = (Integer)pageInfo.get("pageSize");
        Integer pageNum = (Integer)pageInfo.get("pageNum");
        Integer key2 = (Integer) map.get("key2");
        PageInfo<TbOrder> pageInfo2 = orderListService.selOrderByDepartment(pageNum,pageSize,key2);
        return pageInfo2!=null?ResultVO.success("success",pageInfo2):ResultVO.failure("error");
    }
    @GetMapping("/order/delOrder/{oid}")
    public ResultVO softdelOrder(@PathVariable("oid") Integer oid){
        int i = orderListService.updateOrderSoftdel(oid);
        return i>0?ResultVO.success("success"):ResultVO.failure("error");
    }
    @GetMapping("/order/getAllactualpay")
    public ResultVO getAllactualpay(){
        int i = orderListService.getAllactualpay();
        return i>=0?ResultVO.success("success",i):ResultVO.failure("error");
    }
    @PostMapping("/order/section/selorder")
    public ResultVO getOrderBySectionSome(@RequestBody Map<String,Object> map, HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Integer pageNum=(Integer) map.get("pageNum");
        Integer pageSize=(Integer) map.get("pageSize");
        String onum=(String)map.get("onum");
        String bnum=(String)map.get("bnum");
        String cname=(String)map.get("cname");
        Integer orderstatus=null;
        if(map.get("orderstatus")!=null&&map.get("orderstatus")!=""){
            orderstatus =Integer.parseInt(map.get("orderstatus").toString());
        }
        PageInfo<TbOrder> orderBySectionSome = orderListService.getOrderBySectionSome(token,pageNum, pageSize,onum, bnum, cname, orderstatus);
        return orderBySectionSome!=null?ResultVO.success("success",orderBySectionSome):ResultVO.failure("error");
    }

    @PostMapping("/order/getAllByCascade")
    public ResultVO getAllByCascade(@RequestBody Map<String, Object> map) {
        List<TbOrder> allOrderByCascade = orderListService.getAllByCascade((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), (List) map.get("cascadeValue"));
        if (allOrderByCascade.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbOrder> pageInfo = new PageInfo(allOrderByCascade);
        return ResultVO.success("查询成功", pageInfo);
    }
}
